package com.example.homecunter;

import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.chaychan.library.BottomBarItem;
import com.chaychan.library.BottomBarLayout;
import com.example.homecunter.databinding.ActivityHomeBinding;
import com.example.homecunter.fragment.utils.FragmentAdapter;
import com.example.homecunter.fragment.utils.TableEntity;
import com.example.homecunter.video.GsyvFragment;
import com.example.homecunter.news.HomeFragment;
import com.example.homecunter.fragment.MyFragment;
import com.example.homecunter.fragment.TouTiaoFragment;
import com.example.mvvmcore.view.BaseActivity;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;

import java.util.ArrayList;

@Route(path = "/home/main")
public class HomeActivity extends BaseActivity {

    ArrayList<CustomTabEntity> mTabEntitys = new ArrayList<>();
    FragmentAdapter fragmentAdapter;


    ActivityHomeBinding binding;


    @Override
    protected void initData() {
        binding = DataBindingUtil.setContentView(this,R.layout.activity_home);
        binding.setHomes(this);

//        PushAgent.getInstance(this).onAppStart();


//        getSupportFragmentManager().beginTransaction().replace()

        ArrayList<Fragment> fragments = new ArrayList<>();

        fragments.add(new HomeFragment());
        fragments.add(new GsyvFragment());
        fragments.add(new TouTiaoFragment());
        fragments.add(new MyFragment());

        mTabEntitys.add(new TableEntity("首页",0,0));
        mTabEntitys.add(new TableEntity("视频",0,0));
        mTabEntitys.add(new TableEntity("微头条",0,0));
        mTabEntitys.add(new TableEntity("我的",0,0));

        fragmentAdapter = new FragmentAdapter(getSupportFragmentManager(), fragments);
        binding.vp.setAdapter(fragmentAdapter);
        binding.tb.setTabData(mTabEntitys);

        binding.tb.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                binding.vp.setCurrentItem(position);
            }

            @Override
            public void onTabReselect(int position) {

            }
        });
        binding.vp.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                binding.tb.setCurrentTab(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

//        binding.bottom.setOnItemSelectedListener(new BottomBarLayout.OnItemSelectedListener() {
//            @Override//currentPosition(当前位置)   previousPosition(之前位置)
//            public void onItemSelected(BottomBarItem bottomBarItem, int previousPosition, int currentPosition) {
//                //当前位置=之前位置  && 当前位置没有被点击过 && 集合内没有添加
//                if (currentPosition==previousPosition&&currentPosition==0&&!fragments.get(currentPosition).isAdded()){
//                    getSupportFragmentManager().beginTransaction().add(R.id.frag,fragments.get(currentPosition)).commit();
//                }
//                else if (currentPosition!=previousPosition&&fragments.get(currentPosition).isAdded()){
//                    getSupportFragmentManager().beginTransaction().hide(fragments.get(previousPosition)).show(fragments.get(currentPosition)).commit();
//                }
//                else if (currentPosition!=previousPosition){
//                    getSupportFragmentManager().beginTransaction().hide(fragments.get(previousPosition)).add(R.id.frag,fragments.get(currentPosition)).commit();
//                }
//            }
//        });
//        binding.bottom.setCurrentItem(0);
    }

}
